package com.wptk.filemanager.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.wptk.filemanager.App;
import com.wptk.filemanager.R;
import com.wptk.filemanager.ad.utils.AnalyticsUtils;
import com.wptk.filemanager.model.Constant;
import com.wptk.filemanager.model.FileDataManager;
import com.wptk.filemanager.ui.clean.AnimationActivity;
import com.wptk.filemanager.ui.clean.ScanActivity;
import com.wptk.filemanager.utils.StorageQueryUtil;
import com.wptk.filemanager.utils.Utils;
import com.wptk.filemanager.ui.clean.BatteryScanActivity;
import com.wptk.filemanager.ui.base.BaseFragment;

public class CleanFragment extends BaseFragment implements View.OnClickListener {


    private LottieAnimationView av;
    private LinearLayout boostLL;
    private LinearLayout coolerLL;
    private LinearLayout batteryLL;
    private Button cleanBtn;
    private TextView bfbTv;
    private TextView storageInfoTv;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_clean;
    }

    @Override
    protected void initView(View rootView) {

        cleanBtn = rootView.findViewById(R.id.clean_btn);
        boostLL = rootView.findViewById(R.id.boost_ll);
        coolerLL = rootView.findViewById(R.id.cooler_ll);
        batteryLL = rootView.findViewById(R.id.battery_ll);
        bfbTv = rootView.findViewById(R.id.bfb_tv);
        storageInfoTv = rootView.findViewById(R.id.storage_info_tv);

    }

    @Override
    protected void initEvent() {

        boostLL.setOnClickListener(this);
        coolerLL.setOnClickListener(this);
        batteryLL.setOnClickListener(this);
        cleanBtn.setOnClickListener(this);

        String[] result = StorageQueryUtil.queryWithStorageManager(Utils.getContext(), 1024);
        bfbTv.setText(result[2]);
        storageInfoTv.setText(result[1]+"/"+result[0]);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(isSpeedClean()){
            cleanBtn.setText(getString(R.string.clean));
        }else{
            cleanBtn.setText(R.string.optimize);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.clean_btn:
                AnalyticsUtils.INSTANCE.logEvent("shiyongql");
                if(!isSpeedClean()){
                    gotoRepAnimation(AnimationActivity.TYPE_CLEAN_REP);
                }else{
                    gotoScan(ScanActivity.TYPE_CLEAN);
                }
                break;
            case R.id.boost_ll:
                AnalyticsUtils.INSTANCE.logEvent("shiyongjs");
                if(!isBoost()){
                    gotoRepAnimation(AnimationActivity.TYPE_BOOST_REP);
                }else{
                    gotoScan(ScanActivity.TYPE_BOOST);
                }
                break;
            case R.id.cooler_ll:
                AnalyticsUtils.INSTANCE.logEvent("shiyongjw");
                if(!isCooler()){
                    gotoRepAnimation(AnimationActivity.TYPE_COOLER_REP);
                }else{
                    gotoScan(ScanActivity.TYPE_COOLER);
                }
                break;
            case R.id.battery_ll:
                AnalyticsUtils.INSTANCE.logEvent("shiyongsd");
                if(!isBattery()){
                    gotoRepAnimation(AnimationActivity.TYPE_BATTERY_REP);
                }else{
                    migrateTo(BatteryScanActivity.class);
                }
                break;
        }
    }

    private void gotoScan(int type) {
        Intent cleanIntent = new Intent(getContext(), ScanActivity.class);
        cleanIntent.putExtra(ScanActivity.TYPE, type);
        startActivity(cleanIntent);
    }

    private void gotoRepAnimation(int type) {
        Intent repeat = new Intent(getContext(), AnimationActivity.class);
        repeat.putExtra(AnimationActivity.TYPE, type);
        startActivity(repeat);
    }

    private boolean isSpeedClean(){
        long lastTime = App.spUtils.getLong(Constant.LAST_CLEAN_TIME, 0);
        long time = System.currentTimeMillis() - lastTime;
        long outTime = FileDataManager.appConfig.getFile_Config().getQhsj() > 0
                ? FileDataManager.appConfig.getFile_Config().getQhsj()
                : 300;
        return ( time > outTime*1000);
    }
    private boolean isBoost(){
        long lastTime = App.spUtils.getLong(Constant.LAST_BOOST_TIME, 0);
        long time = System.currentTimeMillis() - lastTime;
        long outTime = FileDataManager.appConfig.getFile_Config().getQhsj() > 0
                ? FileDataManager.appConfig.getFile_Config().getQhsj()
                : 300;
        return ( time > outTime*1000);
    }
    private boolean isCooler(){
        long lastTime = App.spUtils.getLong(Constant.LAST_COOLER_TIME, 0);
        long time = System.currentTimeMillis() - lastTime;
        long outTime = FileDataManager.appConfig.getFile_Config().getQhsj() > 0
                ? FileDataManager.appConfig.getFile_Config().getQhsj()
                : 300;
        return ( time > outTime*1000);
    }
    private boolean isBattery(){
        long lastTime = App.spUtils.getLong(Constant.LAST_BATTERY_TIME, 0);
        long time = System.currentTimeMillis() - lastTime;
        long outTime = FileDataManager.appConfig.getFile_Config().getQhsj() > 0
                ? FileDataManager.appConfig.getFile_Config().getQhsj()
                : 300;
        return ( time > outTime*1000);
    }

}
